import goodsRules from '../rules/goods-rules'
import declare from '../rules/declare-rules'
import container from '../rules/container'
import relate from '../rules/relate'
import edoc from '../rules/edoc'

const wtReg = /^(?!^0*(\.0*)?$)(\d{1,14}(\.\d{1,5})?)$/
// 合并集装箱校验规则
function assignContainerRules (vm) {
    const rules = {
        containerType (value) {
            let err = container.containerType(value)
            let specialType = ['11', '12', '13', '32']
            let grossWet = vm.getFormByKey('declareForm', 'grossWet')
            grossWet = Number(grossWet)
            if (grossWet && specialType.includes(value)) {
                if (grossWet > 26000) {
                    err = '毛重超过26000KG，请检查'
                }
            }
            // 校验毛重
            let msg = err.includes(26000)
                ? err
                : wtReg.test(grossWet)
                    ? ''
                    : '请输入整数最多14位，小数最多5位且大于0的数字'
            // 设置表头表单错误信息
            vm.setFormTips('declareForm', 'grossWet', msg)
            return err
        }
    }
    return Object.assign({}, container, rules)
}

function validateRate (value, type) {
    if (!/^\d+(\.\d*)?$/.test(value)) return '请输入大于0的数字'
    if (type == 1) {
        const num = Number(value)
        if (num < 0.0001 || num > 99) return '录入值范围为0.0001-99'
    } else if (type == 2) {
        if (!/^(?!^0*(\.0*)?$)(\d{1,10}(\.\d{1,4})?)$/.test(value)) return '整数最多录入10位，小数点后面最多4位，且大于0'
    } else {
        if (!/^(?!^0*(\.0*)?$)(\d{1,12}(\.\d{1,4})?)$/.test(value)) return '整数最多录入12位，小数点后面最多4位，且大于0'
    }
    return ''
}
// 合并报关单校验规则
function assignDeclareRules (vm) {
    const rules = {
        // 运费
        feeMark (value) {
            let err = ''
            if (vm.feeMarkRequired && !value) {
                err = '此项必填'
            }
            return err
        },
        feeRate (value) {
            const feeMark = vm.getFormByKey('declareForm', 'feeMark')
            let err = ''
            if (value) {
                err = validateRate(value, feeMark)
            }
            return err
        },
        // 运费
        insurMark (value) {
            let err = ''
            if (vm.insurMarkRequird && !value) {
                err = '此项必填'
            }
            return err
        },
        insurRate (value) {
            const insurMark = vm.getFormByKey('declareForm', 'insurMark')
            let err = ''
            if (value) {
                err = validateRate(value, insurMark)
            }
            return err
        },
        // 杂费
        otherRate (value) {
            const otherMark = vm.getFormByKey('declareForm', 'otherMark')
            let err = ''
            if (value) {
                err = validateRate(value, otherMark)
            }
            return err
        },
        // 表头毛重(KG)
        grossWet (value) {
            const containerType = vm.getFormByKey('containerForm', 'containerType')
            const flag =
                ['11', '12', '13', '32'].includes(containerType) &&
                Number(value) > 26000
            let err = ''
            if (value) {
                if (wtReg.test(value)) {
                    if (flag) {
                        err = '毛重超过26000KG，请检查'
                    }
                } else {
                    err = '请输入整数最多14位，小数最多5位且大于0的数字'
                }
            } else {
                err = '此项必填'
            }
            // 校验集装箱规格
            if (containerType) {
                const msg = flag ? '毛重超过26000KG，请检查' : ''
                vm.setFormTips('containerForm', 'containerType', msg)
            }
            return err
        },
        ieDate (value) {
            const flag = vm.isImport
            return flag ? (value ? '' : '日期必填') : ''
        },
        goodsPlace (value) {
            const flag = vm.isImport == 'I'
            return value
                ? value.length > 100 ? '货物存放地点长度小于等于100' : ''
                : flag ? '货物存放地点为必填项' : ''
        },
        orgCode (value) {
            const err = vm.getFormTips('declareForm', 'orgCode')
            return value ? '' : err
        },
        vsaOrgCode (value) {
            const err = vm.getFormTips('declareForm', 'vsaOrgCode')
            return value ? '' : err
        },
        inspOrgCode (value) {
            const err = vm.getFormTips('declareForm', 'inspOrgCode')
            return value ? '' : err
        },
        purpOrgCode (value) {
            const err = vm.getFormTips('declareForm', 'purpOrgCode')
            return value ? '' : err
        },
        despDate (value) {
            const err = vm.getFormTips('declareForm', 'despDate')
            return value ? '' : err
        }
    }
    return Object.assign({}, declare, rules)
}

const edocRules = Object.freeze(edoc)
const relRules = Object.freeze(relate)
export default {
    data () {
        const declareRules = assignDeclareRules(this)
        const containerRules = assignContainerRules(this)
        return {
            containerRules: Object.freeze(containerRules),
            goodsRules: Object.freeze(goodsRules),
            declareRules: Object.freeze(declareRules),
            edocRules,
            relRules
        }
    }
}
